package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.domain.New;
import cn.wolfcode.trip.base.domain.New;
import cn.wolfcode.trip.base.mapper.NewContentMapper;
import cn.wolfcode.trip.base.mapper.NewMapper;
import cn.wolfcode.trip.base.mapper.UserNewMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewServiceImpl implements INewService {
    @Autowired
    private NewMapper newMapper;
    @Autowired
    private NewContentMapper contentMapper;
    @Autowired
    private UserNewMapper userNewMapper;


    public PageInfo<New> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<New> list = newMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(New entity) {
        //设置发布时间
        if (entity.getState() == New.STATE_RELEASE) {
            entity.setReleaseTime(new Date());
        }
        //文章内容的主键关联
        NewContent newContent = entity.getNewContent();
        if (entity.getId() == null) {
            //设置创建时间
            entity.setCreateTime(new Date());
            newMapper.insert(entity);
            newContent.setId(entity.getId());
            contentMapper.insert(newContent);
        } else {
            newMapper.updateByPrimaryKey(entity);
            newContent.setId(entity.getId());
            contentMapper.updateByPrimaryKey(newContent);
        }
    }

    @Override
    public String getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id).getContent();
    }

   /* public List<New> list() {
        return newMapper.selectEnable();
    }*/

    public New get(Long id) {
        //1:根据id查询到游记对象
        New aNew = newMapper.selectByPrimaryKey(id);
        //2:根据游记的id查询内容对象,设置到游记中
        NewContent newContent = contentMapper.selectByPrimaryKey(id);
        aNew.setNewContent(newContent);
        return aNew;
    }

    @Override
    public New getNewByMaxDate(Long id) {
        return newMapper.selectByMaxDate(id);
    }

    @Override
    public void saveNewsDetailToUser(Long userId, Long newsId) {
        //先查询,
        UserNew userNew=userNewMapper.getNewsDetailToUser(userId,newsId);
        //如果有数据就跳过
        if(userNew==null){
            userNewMapper.saveNewsDetailToUser(userId,newsId);
        }
    }

    @Override
    public void deleteNewsDetailToUser(Long userId, Long newsId) {
        //先查询,
        UserNew userNew=userNewMapper.getNewsDetailToUser(userId,newsId);
        //如果有数据就跳过
        if(userNew!=null){
        userNewMapper.deleteNewsDetailToUser(userId,newsId);
        }
    }

    @Override
    public String getNewsForUser(Long userId, Long newsId) {
        UserNew userNew=userNewMapper.getNewsDetailToUser(userId,newsId);
        if(userNew==null){
            return "FALSE";
        }
        return "TRUE";
    }


    //查询当前用户收藏的所有日报
    @Override
    public List<New> getAllNewsByUser(Long userId) {
      List<New> news= userNewMapper.getAllNewsByUser(userId);

        return news;
    }

}
